package great.easychat.help

import androidx.lifecycle.Lifecycle
import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.ObservableTransformer
import org.greenrobot.eventbus.EventBus

/**
 * @author athoucai
 * @date 2018/9/7
 */

internal interface IActivityProxy {


    /**
     * 修改主题theme
     */
    fun changeTheme()

    /**
     * rxjava绑定生命周期
     * @param lifeCycleEvent
     * @param <T>
     * @return
    </T> */
    fun <T> bindUntilEvent(lifeCycleEvent: Lifecycle.Event): LifecycleTransformer<T>

    /**
     * 注册eventbus
     * @param object
     */
    fun registerEventBus(any: Any) {
        if (!EventBus.getDefault().isRegistered(any)) {
            EventBus.getDefault().register(any)
        }
    }

    /**
     * 取消注册eventbus
     * @param object
     */
    fun unregisterEventBus(any: Any) {
        if (EventBus.getDefault().isRegistered(any)) {
            EventBus.getDefault().unregister(any)
        }
    }

    class LifecycleTransformer<T> internal constructor(internal val observable: Observable<*>) : ObservableTransformer<T, T> {

        override fun apply(upstream: Observable<T>): ObservableSource<T> {
            return upstream.takeUntil(observable)
        }
    }
}
